package bamin.com.kepiao.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import bamin.com.kepiao.activity.PayActivity;
import bamin.com.kepiao.constant.Constant;

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler{
    private String TAG = "WXPayEntryActivity";
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constant.WechatPay.APP_ID);
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {
        Log.e("onResp ", "onResp 微信回调");
        sendPayResult(resp.errCode);
        finish();
    }

    private void sendPayResult(int errCode) {
        Intent intent = new Intent(PayActivity.PAY_RECEIVER);
        intent.putExtra("error", errCode);
        sendBroadcast(intent);
    }

}